package com.blame.springblog.common.utils;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.io.Encoders;
import io.jsonwebtoken.security.Keys;
import jakarta.websocket.Decoder;
import jakarta.websocket.Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.xml.crypto.dsig.spec.SignatureMethodParameterSpec;
import java.security.Key;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtils {

    private static final Logger log = LoggerFactory.getLogger(JwtUtils.class);
    private static  String SECRET_STRING="2FDVor3CaEMpuG4vQ7T6BACe31F7cqPt09OSXEPQlGc=";
    private static  Key key=Keys.hmacShaKeyFor(Decoders.BASE64.decode(SECRET_STRING));



    public static String genJwt(Map<String,Object> chaims){
        String jpt=Jwts.builder()
                .setClaims(chaims)
                .signWith(key)
                .compact();
        return  jpt;
    }

    public static Claims parseToken(String token){
        if(!StringUtils.hasLength(token)){
            return null;
        }

        JwtParser build=Jwts.parserBuilder().setSigningKey(key).build();
        Claims claims=null;
        try{
            claims=build.parseClaimsJws(token).getBody();
        }catch (Exception e){
            log.error("token解析失败"+token);
        }
        return claims;

    }
}
